import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.*;

public class AdminMainFrame extends JFrame {
    // 模拟数据存储
    private java.util.List<Student> studentList = new ArrayList<>();
    private java.util.List<Event> eventList = new ArrayList<>();
    private java.util.List<Registration> volunteerList = new ArrayList<>();

    // 学生管理组件
    private DefaultTableModel studentTableModel;
    private JTable studentTable;
    private JTextField studentIdField, studentNameField;

    // 项目管理组件
    private DefaultTableModel eventTableModel;
    private JTable eventTable;
    private JTextField eventNameField, eventRuleField, eventLocationField;
    private JTextField eventStartField, eventEndField; // 用简单字符串模拟日期

    // 志愿者查询组件
    private DefaultTableModel volunteerTableModel;
    private JTable volunteerTable;

    public AdminMainFrame() {
        setTitle("河南工业大学运动会管理系统 - 管理员主界面");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setBackground(new Color(255, 182, 193));

        JTabbedPane tabbedPane = new JTabbedPane();

        // 1. 学生管理面板
        tabbedPane.addTab("学生管理", createStudentManagementPanel());

        // 2. 项目管理面板
        tabbedPane.addTab("运动项目管理", createEventManagementPanel());

        // 3. 志愿者查询
        tabbedPane.addTab("志愿者查询", createVolunteerQueryPanel());

        add(tabbedPane, BorderLayout.CENTER);
    }

    private JPanel createStudentManagementPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 表格
        studentTableModel = new DefaultTableModel(new String[]{"学号", "姓名", "报名项目"}, 0);
        studentTable = new JTable(studentTableModel);
        JScrollPane scrollPane = new JScrollPane(studentTable);
        panel.add(scrollPane, BorderLayout.CENTER);

        // 表单和按钮
        JPanel formPanel = new JPanel(new GridBagLayout());
        formPanel.setBackground(new Color(255, 192, 203)); // 淡粉

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(6, 6, 6, 6);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        formPanel.add(new JLabel("学号:"), gbc);
        studentIdField = new JTextField(12);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(studentIdField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.EAST;
        formPanel.add(new JLabel("姓名:"), gbc);
        studentNameField = new JTextField(12);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(studentNameField, gbc);

        JButton addBtn = new JButton("添加学生");
        JButton deleteBtn = new JButton("删除学生");
        JButton refreshBtn = new JButton("刷新列表");

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;

        JPanel btnPanel = new JPanel();
        btnPanel.setBackground(new Color(255, 192, 203));
        btnPanel.add(addBtn);
        btnPanel.add(deleteBtn);
        btnPanel.add(refreshBtn);
        formPanel.add(btnPanel, gbc);

        panel.add(formPanel, BorderLayout.SOUTH);

        // 按钮事件
        addBtn.addActionListener(e -> {
            String id = studentIdField.getText().trim();
            String name = studentNameField.getText().trim();
            if (id.isEmpty() || name.isEmpty()) {
                JOptionPane.showMessageDialog(this, "学号和姓名不能为空！");
                return;
            }
            for (Student s : studentList) {
                if (s.id.equals(id)) {
                    JOptionPane.showMessageDialog(this, "该学号已存在！");
                    return;
                }
            }
            studentList.add(new Student(id, name));
            JOptionPane.showMessageDialog(this, "学生添加成功");
            refreshStudentTable();
        });

        deleteBtn.addActionListener(e -> {
            int row = studentTable.getSelectedRow();
            if (row == -1) {
                JOptionPane.showMessageDialog(this, "请先选中要删除的学生");
                return;
            }
            String delId = (String) studentTableModel.getValueAt(row, 0);
            studentList.removeIf(s -> s.id.equals(delId));
            JOptionPane.showMessageDialog(this, "删除成功");
            refreshStudentTable();
        });

        refreshBtn.addActionListener(e -> refreshStudentTable());

        refreshStudentTable();

        return panel;
    }

    private void refreshStudentTable() {
        studentTableModel.setRowCount(0);
        for (Student s : studentList) {
            String projects = String.join((CharSequence) ", ", (CharSequence) Collections.singleton(s.getRegisteredEventNames()));
            studentTableModel.addRow(new Object[]{s.id, s.name, projects});
        }
    }

    private JPanel createEventManagementPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        eventTableModel = new DefaultTableModel(new String[]{"项目名", "规则", "地点", "开始时间", "结束时间"}, 0);
        eventTable = new JTable(eventTableModel);
        JScrollPane scrollPane = new JScrollPane(eventTable);
        panel.add(scrollPane, BorderLayout.CENTER);

        JPanel formPanel = new JPanel(new GridBagLayout());
        formPanel.setBackground(new Color(255, 192, 203));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        formPanel.add(new JLabel("项目名称:"), gbc);
        eventNameField = new JTextField(15);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(eventNameField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.EAST;
        formPanel.add(new JLabel("规则:"), gbc);
        eventRuleField = new JTextField(15);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(eventRuleField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.EAST;
        formPanel.add(new JLabel("地点:"), gbc);
        eventLocationField = new JTextField(15);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(eventLocationField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.EAST;
        formPanel.add(new JLabel("开始时间:"), gbc);
        eventStartField = new JTextField(15);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(eventStartField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.EAST;
        formPanel.add(new JLabel("结束时间:"), gbc);
        eventEndField = new JTextField(15);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(eventEndField, gbc);

        JButton addEventBtn = new JButton("发布项目");
        JButton refreshEventBtn = new JButton("刷新项目列表");
        JButton deleteEventBtn = new JButton("删除选中项目");

        JPanel btnPanel = new JPanel();
        btnPanel.setBackground(new Color(255, 192, 203));
        btnPanel.add(addEventBtn);
        btnPanel.add(deleteEventBtn);
        btnPanel.add(refreshEventBtn);

        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        formPanel.add(btnPanel, gbc);

        panel.add(formPanel, BorderLayout.SOUTH);

        // 事件监听
        addEventBtn.addActionListener(e -> {
            String name = eventNameField.getText().trim();
            String rule = eventRuleField.getText().trim();
            String location = eventLocationField.getText().trim();
            String start = eventStartField.getText().trim();
            String end = eventEndField.getText().trim();

            if (name.isEmpty() || rule.isEmpty() || location.isEmpty() || start.isEmpty() || end.isEmpty()) {
                JOptionPane.showMessageDialog(this, "所有项目字段不能为空！");
                return;
            }

            eventList.add(new Event(name, rule, location, start, end));
            JOptionPane.showMessageDialog(this, "项目发布成功！");
            refreshEventTable();
        });

        refreshEventBtn.addActionListener(e -> refreshEventTable());

        deleteEventBtn.addActionListener(e -> {
            int row = eventTable.getSelectedRow();
            if (row == -1) {
                JOptionPane.showMessageDialog(this, "请选择要删除的项目");
                return;
            }
            String delName = (String) eventTableModel.getValueAt(row, 0);
            eventList.removeIf(ev -> ev.name.equals(delName));
            JOptionPane.showMessageDialog(this, "删除成功");
            refreshEventTable();
        });

        refreshEventTable();

        return panel;
    }

    private void refreshEventTable() {
        eventTableModel.setRowCount(0);
        for (Event ev : eventList) {
            eventTableModel.addRow(new Object[]{ev.name, ev.rule, ev.location, ev.startTime, ev.endTime});
        }
    }

    private JPanel createVolunteerQueryPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        volunteerTableModel = new DefaultTableModel(new String[]{"志愿者账号", "报名项目"}, 0);
        volunteerTable = new JTable(volunteerTableModel);
        JScrollPane scrollPane = new JScrollPane(volunteerTable);
        panel.add(scrollPane, BorderLayout.CENTER);

        JButton refreshVolBtn = new JButton("刷新志愿者列表");
        JPanel btnPanel = new JPanel();
        btnPanel.setBackground(new Color(255, 192, 203));
        btnPanel.add(refreshVolBtn);
        panel.add(btnPanel, BorderLayout.SOUTH);

        refreshVolBtn.addActionListener(e -> refreshVolunteerTable());

        refreshVolunteerTable();

        return panel;
    }

    private void refreshVolunteerTable() {
        volunteerTableModel.setRowCount(0);
        for (Registration reg : volunteerList) {
            if (reg.type.equals("volunteer")) {
                volunteerTableModel.addRow(new Object[]{reg.userId, reg.eventName});
            }
        }
    }

    // 内部类模拟学生、项目、报名

}

